package com.example.shouybl.fragment;


import android.content.Intent;
import android.os.Bundle;

import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.TypeReference;
import com.example.shouybl.BaseApp;
import com.example.shouybl.R;
import com.example.shouybl.adapter.MoneyAdapter;
import com.example.shouybl.bean.AppBaseBean;
import com.example.shouybl.bean.MoneyData;
import com.example.shouybl.constant.Path;
import com.example.shouybl.ui.AwardActivity;
import com.example.shouybl.ui.MoneyDetailActivity;
import com.example.shouybl.utils.AppAsyncTaskHelper;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.loopj.android.http.RequestParams;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class MoneyFragment extends Fragment {


    private PullToRefreshListView ptrMoney;
    private ListView lvMoney;
    private List<MoneyData> moneyDatas=new ArrayList<MoneyData>();
    private MoneyAdapter moneyAdapter;
    private int page=1;
    private RadioGroup radioGroup;
    private RelativeLayout layout;

    //title布局相关
    private ImageView ivLeft;
    private TextView tvTitle;
    private Button btnRight;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
      View view= inflater.inflate(R.layout.fragment_money, container, false);
        initView(view);
        addHead(inflater);
        initData();
        ptrMoney.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                page = 1;
                initData();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                page++;
                initData();
            }
        });

        lvMoney.setAdapter(moneyAdapter);
        lvMoney.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent=new Intent(BaseApp.getInstance(), MoneyDetailActivity.class);
                Bundle bundle=new Bundle();
                bundle.putSerializable("money",moneyDatas.get(position-2));
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
        return view;
    }

    private void addHead(LayoutInflater inflater) {
        layout = (RelativeLayout) inflater.inflate(R.layout.item_money_head_layout, null);
        radioGroup = (RadioGroup) layout.findViewById(R.id.money_rg);
        RadioButton button= (RadioButton) layout.findViewById(R.id.money_store_btn);
        button.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Intent intent=new Intent(BaseApp.getInstance(), AwardActivity.class);
                startActivity(intent);
            }
        });

        lvMoney.addHeaderView(layout);
    }

    private void initData() {
        TypeReference<AppBaseBean<MoneyData>> typeReference=new TypeReference<AppBaseBean<MoneyData>>(){};
        AppAsyncTaskHelper.getInstance().post(Path.PATH_MONEY,getParams(),moneyDatas,ptrMoney,moneyAdapter,page,typeReference);
    }

    private RequestParams getParams() {
        RequestParams params=new RequestParams();
        params.add("platform","2");
        params.add("page",String.valueOf(page));
        return params;
    }

    private void initView(View view) {
        //主体变量
        ptrMoney = (PullToRefreshListView) view.findViewById(R.id.ptr_money);
        ptrMoney.setMode(PullToRefreshBase.Mode.BOTH);
        lvMoney = ptrMoney.getRefreshableView();
        moneyAdapter = new MoneyAdapter(moneyDatas, BaseApp.getInstance());
        //title变量
        ivLeft = (ImageView) view.findViewById(R.id.iv_left);
        tvTitle = (TextView) view.findViewById(R.id.tv_title);
        btnRight = (Button) view.findViewById(R.id.btn_right);
        tvTitle.setText("搜游戏");
        btnRight.setText("请登录");
    }

}
